# -*- coding:utf-8 -*-
"""
Author：Administrator
Date:2023年02月23日
"""

import docx
from docx.shared import Cm, Pt
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT


def setAttrs(rootElement, elements, attrs):
	for element in elements:
		myElement = rootElement.find(docx.oxml.ns.qn(element))
		if myElement is None:
			myElement = docx.oxml.OxmlElement(element)
			rootElement.append(myElement)
		for key in attrs:
			myElement.set(docx.oxml.ns.qn(key), attrs[key])


# 自定义单元格边框颜色的函数
def setCellBorderColor(row, col, myColor):
	myTcPr = myTable.cell(row, col)._tc.get_or_add_tcPr()
	myTcBorders = myTcPr.first_child_found_in('w:tcBorders')
	if myTcBorders is None:
		myTcBorders = docx.oxml.OxmlElement('w:tcBorders')
		myTcPr.append(myTcBorders)
	for myEdge in ('left', 'right', 'bottom', 'top'):
		myEdgeData = {'color': myColor}
		if myEdgeData:
			myTag = 'w:{}'.format(myEdge)
			myElement = myTcBorders.find(docx.oxml.ns.qn(myTag))
			if myElement is None:
				myElement = docx.oxml.OxmlElement(myTag)
			myTcBorders.append(myElement)
			myElement.set(docx.oxml.ns.qn('w:{}'.format('color')), str(myEdgeData['color']))


# 自定义单元格边框的函数
def setCellBorder(row, col, **borderArgs):
	myTcPr = myTable.cell(row, col)._tc.get_or_add_tcPr()
	myTcBorders = myTcPr.first_child_found_in('w:tcBorders')
	if myTcBorders is None:
		myTcBorders = docx.oxml.OxmlElement('w:tcBorders')
		myTcPr.append(myTcBorders)
	for myEdge in ('left', 'right', 'top', 'bottom'):
		myEdgeData = borderArgs.get(myEdge)
		if myEdgeData:
			myTag = 'w:{}'.format(myEdge)
			myElement = myTcBorders.find(docx.oxml.ns.qn(myTag))
			if myElement is None:
				myElement = docx.oxml.OxmlElement(myTag)
			myTcBorders.append(myElement)
			for myKey in ['sz', 'val', 'color']:
				if myKey in myEdgeData:
					myElement.set(docx.oxml.ns.qn('w:{}'.format(myKey)), str(myEdgeData[myKey]))


myDocument = docx.Document('新员工.docx')
myTable = myDocument.tables[0]
for i in range(len(myTable.rows)):
	for j in range(len(myTable.columns)):
		setCellBorder(i, j, top={'sz': 12, 'val': "double", 'color': "#ff0000"},
		              bottom={'sz': 12, 'val': "double", 'color': "#ff0000"},
		              left={'sz': 12, 'val': "double", 'color': "#ff0000"},
		              right={'sz': 12, 'val': "double", 'color': "#ff0000"}, )

myDocument.save('02散文名篇.docx')
